﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;


/// <summary>
/// Summary description for Class1
/// </summary>
public class FileUploadHandler : IHttpHandler
{
    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            //Uploaded File Deletion
            if (context.Request.QueryString.Count > 0)
            {
                string filePath = @"c:\DownloadedFiles\" + context.Request.QueryString[0].ToString();
                if (File.Exists(filePath))
                    File.Delete(filePath);
            }
            //File Upload
            else
            {
                //context.Request.Entidad.Id
                //context.Request.Entidad.Name
                string fileName = Path.GetFileName(context.Request.Files[0].FileName);
                string rootPath = HttpContext.Current.Server.MapPath("~");
                string location = rootPath.Replace("\\", "/") + @"/Imagenes/temp/" + fileName;
                context.Request.Files[0].SaveAs(location);

            }
        }
        catch (Exception ex)
        {
            LogErrorServices logErrorService = new LogErrorServices();
            logErrorService.InsertarLogError(ex.GetType().ToString(), ex.StackTrace, ex.Message, ex.GetBaseException().ToString(), ex.Source);
        }
    }

    #endregion
}